//-----------------------------------------------------------------------
// <copyright file="PermissionDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class PermissionDAOBase
    {
        #region Common methods
        public virtual Permission CreatePermissionFromReader(IDataReader reader)
        {
            Permission item = new Permission();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("PermissionId"))) item.PermissionId = (int)reader["PermissionId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) item.Code = (string)reader["Code"];
                if (!reader.IsDBNull(reader.GetOrdinal("PermissionName"))) item.PermissionName = (string)reader["PermissionName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("Role"))) item.Role = (string)reader["Role"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatePermissionFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatePermission methods
            
        public virtual void CreatePermission(Permission permission)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPermissionInsert");
                
                database.AddInParameter(dbCommand, "@Code", DbType.String, permission.Code);
                database.AddInParameter(dbCommand, "@PermissionName", DbType.String, permission.PermissionName);
                database.AddInParameter(dbCommand, "@Description", DbType.String, permission.Description);
                database.AddInParameter(dbCommand, "@Role", DbType.String, permission.Role);
                database.AddOutParameter(dbCommand, "@PermissionId", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                permission.PermissionId = (int)database.GetParameterValue(dbCommand, "@PermissionId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatePermissionException, ex);
            }
        }

        #endregion

        #region UpdatePermission methods
        
        public virtual void UpdatePermission(Permission permission)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPermissionUpdate");            
                
                database.AddInParameter(dbCommand, "@PermissionId", DbType.Int32, permission.PermissionId);
                database.AddInParameter(dbCommand, "@Code", DbType.String, permission.Code);
                database.AddInParameter(dbCommand, "@PermissionName", DbType.String, permission.PermissionName);
                database.AddInParameter(dbCommand, "@Description", DbType.String, permission.Description);
                database.AddInParameter(dbCommand, "@Role", DbType.String, permission.Role);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatePermissionException, ex);
            }
        }
        
        #endregion

        #region DeletePermission methods
        public virtual void DeletePermission(int permissionId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPermissionDelete");
                
                database.AddInParameter(dbCommand, "@PermissionId", DbType.Int32, permissionId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletePermissionException, ex);
            }
        }

        #endregion

        #region GetPermission methods
        
        public virtual Permission GetPermission(int permissionId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPermissionGet");
                
                database.AddInParameter(dbCommand, "@PermissionId", DbType.Int32, permissionId);
                
                Permission permission =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        permission = CreatePermissionFromReader(reader);
                        reader.Close();
                    }
                }
                return permission;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetPermissionException, ex);
            }
        }
        
        #endregion

        #region GetPermissionList methods
        public virtual PermissionCollection GetPermissionList(PermissionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPermissionGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                PermissionCollection permissionCollection = new PermissionCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Permission permission = CreatePermissionFromReader(reader);
                        permissionCollection.Add(permission);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return permissionCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetPermissionListException, ex);
            }
        }
        
        public virtual PermissionCollection GetPermissionList(PermissionColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetPermissionList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetPermissionList methods
        public virtual PermissionCollection GetPermissionListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPermissionGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                PermissionCollection permissionCollection = new PermissionCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Permission permission = CreatePermissionFromReader(reader);
                        permissionCollection.Add(permission);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return permissionCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetPermissionListException, ex);
            }
        }
        
        public virtual PermissionCollection GetPermissionListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetPermissionListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}